{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7-1 配对样本均数 t 检验\n",
    "\n",
    "评估咖啡因对心肌血流量的影响，数据如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (12, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>id</th><th>before</th><th>after</th></tr><tr><td>i64</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>4.8</td><td>4.8</td></tr><tr><td>2</td><td>5.1</td><td>4.9</td></tr><tr><td>3</td><td>6.4</td><td>4.5</td></tr><tr><td>4</td><td>5.7</td><td>5.4</td></tr><tr><td>5</td><td>5.6</td><td>4.7</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>8</td><td>4.9</td><td>3.2</td></tr><tr><td>9</td><td>4.7</td><td>3.0</td></tr><tr><td>10</td><td>3.5</td><td>3.2</td></tr><tr><td>11</td><td>5.2</td><td>5.3</td></tr><tr><td>12</td><td>5.3</td><td>5.1</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (12, 3)\n",
       "┌─────┬────────┬───────┐\n",
       "│ id  ┆ before ┆ after │\n",
       "│ --- ┆ ---    ┆ ---   │\n",
       "│ i64 ┆ f64    ┆ f64   │\n",
       "╞═════╪════════╪═══════╡\n",
       "│ 1   ┆ 4.8    ┆ 4.8   │\n",
       "│ 2   ┆ 5.1    ┆ 4.9   │\n",
       "│ 3   ┆ 6.4    ┆ 4.5   │\n",
       "│ 4   ┆ 5.7    ┆ 5.4   │\n",
       "│ 5   ┆ 5.6    ┆ 4.7   │\n",
       "│ …   ┆ …      ┆ …     │\n",
       "│ 8   ┆ 4.9    ┆ 3.2   │\n",
       "│ 9   ┆ 4.7    ┆ 3.0   │\n",
       "│ 10  ┆ 3.5    ┆ 3.2   │\n",
       "│ 11  ┆ 5.2    ┆ 5.3   │\n",
       "│ 12  ┆ 5.3    ┆ 5.1   │\n",
       "└─────┴────────┴───────┘"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import polars as pl\n",
    "\n",
    "df = pl.read_csv(\"B_07_1-data.csv\")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"before\" 和 \"after\" 两样本的数据可以两两配对，而不是相互独立。因而采用配对样本均数 t 检验。\n",
    "\n",
    "统计结果如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t-statistic: [3.73988703]\n",
      "p-value: [0.00326682]\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import ttest_rel\n",
    "\n",
    "result = ttest_rel(df.select(\"before\"), df.select(\"after\"))\n",
    "\n",
    "print(f\"t-statistic: {result.statistic}\\np-value: {result.pvalue}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P < 0.05，因而有 95% 的把握拒绝原假设，认为咖啡因对心肌血流量有显著影响。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
